import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class KeyboardCloseController {
  late void Function() close;
}

class KeyboardClose extends StatelessWidget {
  final Widget child;
  final KeyboardCloseController? controller;

  KeyboardClose({super.key, required this.child, this.controller}) {
    if (controller != null) {
      controller!.close = handleCloseKeyboard;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => handleCloseKeyboard(),
      onTapDown: (TapDownDetails e) => handleCloseKeyboard(),
      child: child,
    );
  }

  void handleCloseKeyboard() {
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }
}
